package cn.sanshuilin.kst.mobile.client.controller;

import cn.sanshuilin.kst.common.api.OperationPlatform;
import cn.sanshuilin.kst.common.utils.DEncrypt;
import cn.sanshuilin.kst.common.utils.IdGenerator;
import cn.sanshuilin.kst.common.utils.StringUtil;
import cn.sanshuilin.kst.mobile.client.constant.Message;
import cn.sanshuilin.kst.mobile.client.constant.ReturnCode;
import cn.sanshuilin.kst.system.api.exception.InvalidCaptchaException;
import cn.sanshuilin.kst.system.api.service.ICaptchaService;
import cn.sanshuilin.kst.system.api.service.IMemberService;
import cn.sanshuilin.kst.system.provider.bean.Member;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.HashMap;
import java.util.Map;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2018-01-29 22:04
 * Copyright 2017  by 三水林科技有限公司
 **/
@Validated
@RestController
@RequestMapping( value = "/{version}/service/member/" )
@Api(tags = "用户管理1")
public class MemberController {
    @Autowired
    private IMemberService memberService;
    /**
     * 验证码服务
     */
    @Autowired
    private ICaptchaService captchaService;
    @PostMapping(value = "regist", produces = "application/json; charset=UTF-8")
    @ApiOperation(value = "注册用户")
    public Map<String, Object> regist(
         @ApiParam(required = true,value = "版本",defaultValue = "v1")@PathVariable("version") String version,
         @Length(min = 6, max = 20, message = "密码长度为6到20")
         @ApiParam(required = true, value = "密码") @RequestParam("password") String password,
         @Pattern(regexp = "1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}", message = "手机号码格式错误")
         @ApiParam(required = true, value = "手机号") @RequestParam("username") String username,
         @Pattern(regexp = "\\d{6}", message = "验证码为6位数字")
         @ApiParam(required = true, value = "短信验证码") @RequestParam("code") String code,
         @ApiParam(required = true,value = "来源") @RequestParam("platform")String platform,
         @ApiParam(required = true,value = "设备号") @RequestParam("deviceId")String deviceId
    )throws InvalidCaptchaException {
        //校验验证码
        captchaService.validCaptcha(username, code);
        //检查库中是否存在你该用户
        Member member = memberService._getMemberByUsername(username);
        if(member != null){
            Map<String, Object> message = new HashMap<>();
            message.put(Message.RETURN_FIELD_CODE, ReturnCode.USER_EXIST);
            return message;
        }
        //创建新用户
        member = new Member();
        // 生成md5密码
        String md5Password = StringUtil.md5EncryptDouble(password);
        member.setId(IdGenerator.INSTANCE.nextId());
        member.setPlatform(platform);
        member.setUsername(username);
        member.setMobile(username);
        member.setPassword(md5Password);
        member.setName(username);
        if(member.getImageId()==null){
            member.setImageId("2c9acabc5e6ef92d015e6efe965c0000");
        }
        if(StringUtils.isEmpty(String.valueOf(member.getGroupId()))){
//            member.setGroupId(CDict.MemberVipGroup.getId());
        }
        memberService.saveMember(member);

        Map<String, Object> message = new HashMap<>();
        message.put(Message.RETURN_FIELD_CODE, ReturnCode.SUCCESS);
        return message;
    }

    @PostMapping(value = "login",produces = "application/json; charset=UTF-8")
    @ApiOperation(value = "用户登录")
    public Map login(
            @ApiParam(required = true,value = "版本",defaultValue = "v1")@PathVariable("version") String version,
            @Pattern(regexp = "1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}", message = "手机号码格式错误")
            @ApiParam(required = true,value = "手机号")@RequestParam("mobile")String mobile,
            @Length(min = 6, max = 20, message = "密码长度为6到20")
            @ApiParam(required = true,value = "密码")@RequestParam("password")String password,
            @ApiParam(required = true,value = "平台")@RequestParam("platform")String platform,
            @ApiParam(required = true,value = "唯一设备号")@RequestParam("deviceId")String deviceId

    ){
        Member member = memberService.login(mobile, password, OperationPlatform.getByPlatformChinese(platform), deviceId);
        if (null==member){
            Map<String, Object> message = new HashMap<>();
            message.put(Message.RETURN_FIELD_CODE, ReturnCode.USER_NOT_EXIST);
            message.put(Message.RETURN_FIELD_DATA, "用户名或密码错误");
            return message;
        }
        Map<String, Object> message = new HashMap<>();
        message.put(Message.RETURN_FIELD_CODE, ReturnCode.SUCCESS);
        message.put(Message.RETURN_FIELD_DATA, member.getToken() );
        return message;
    }

}
